Skip to content

fix: add missing fflush(stdout) to write log message immediately#442

Merged
stackia merged 1 commit intostackia:mainfrom
lark:logger_fix
Apr 8, 2026
Merged

fix: add missing fflush(stdout) to write log message immediately#442
stackia merged 1 commit intostackia:mainfrom
lark:logger_fix

Conversation

@lark
Copy link
Copy Markdown
Contributor

@lark lark commented Apr 8, 2026

Without fflush(stdout), log messages have incorrect timestamp (added by syslogd or journald), or even get printed out when exit() but not startup.

Without fflush(stdout), log messages have incorrect timestamp
(added by syslogd or journald), or even get printed out when exit()
but not startup.
@stackia
Copy link
Copy Markdown
Owner

stackia commented Apr 8, 2026

谢谢,确实是个 bug

@stackia stackia merged commit 201a12c into stackia:main Apr 8, 2026
6 of 7 checks passed
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR aims to make stdout-based logging appear immediately (so external log collectors don’t assign misleading timestamps) by flushing stdout in the logger() helper.

Changes:

  • Flush stdout after writing a log line to ensure it is emitted immediately.
  • Adjust logger()’s doc comment and attempt to account for the auto-appended newline in the return value.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

* @param level Message log level
* @param format printf style format string
* @returns Whatever printf returns
* @return Message length (newline included)
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The updated docstring claims this function returns the message length (newline included), but the implementation still returns fputs()'s status code (and potentially modified by later logic). Either adjust the implementation to return the actual number of characters written (based on the rendered message and whether a newline was appended), or keep the original semantics and document it as a fputs()-style return (non-negative on success, EOF on error).

Suggested change
* @return Message length (newline included)
* @return Non-negative on success, EOF on output error

Copilot uses AI. Check for mistakes.
Comment on lines 122 to +123
fputc('\n', stdout);
r++;
Copy link

Copilot AI Apr 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

r++ here assumes r is a character count, but r is currently the return value from fputs() (a non-negative/EOF status). Incrementing it can also mask an EOF from fputs() (e.g., -1 becomes 0), changing error signaling. Suggestion: remove the r++ unless you change r to track bytes written, and if you do, ensure errors from fputs()/fputc() aren’t lost.

Suggested change
fputc('\n', stdout);
r++;
if (fputc('\n', stdout) == EOF) {
r = EOF;
}

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants